﻿using SinavSistemi.Class;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SinavSistemi
{
    public partial class kullanicilar : System.Web.UI.Page
    {
        string enddate = dateunit.sadecetarih();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {


                SqlConnection sqlcon = connection.baglanti;
                SqlCommand sqlcmd = new SqlCommand("select * from firmalar ", sqlcon);
                sqlcon.Open();
                SqlDataReader sdr = sqlcmd.ExecuteReader();

                ddlFirmalar.DataSource = sdr;
                ddlFirmalar.DataTextField = "firmaadi";
                ddlFirmalar.DataValueField = "firmaID";
                ddlFirmalar.DataBind();

                sqlcon.Close();

             

                SqlCommand sqlcmd2 = new SqlCommand("select * from sinavlar ", sqlcon);
                sqlcon.Open();

                SqlDataReader sdr2 = sqlcmd2.ExecuteReader();
                ddlSinavlar.DataSource = sdr2;
                ddlSinavlar.DataTextField = "sinavismi";
                ddlSinavlar.DataValueField = "sinavID";
                ddlSinavlar.DataBind();
                sqlcon.Close();

              
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string a = dateunit.verilen_tarih(Calendar1.SelectedDate.ToString("d"));
            string link = txtEmail.Text.Replace('@', '-') + DateTime.Now.ToString("s").Replace(':', '-');
            string reallink = "http://www.usadanismanlikegitim.com/Login.aspx?pid=" + link;
            SqlConnection sqlcon = connection.baglanti;
            SqlCommand sqlcmd = new SqlCommand("insertuser", sqlcon);
            sqlcmd.Parameters.AddWithValue("@firmaID", ddlFirmalar.SelectedValue);
            sqlcmd.Parameters.AddWithValue("@adsoyad",  txtAdsoyad.Text);
            sqlcmd.Parameters.AddWithValue("@email",    txtEmail.Text);
            sqlcmd.Parameters.AddWithValue("@username", link);
            sqlcmd.Parameters.AddWithValue("@password", "usaegitim");
            sqlcmd.Parameters.AddWithValue("@startdate",dateunit.sadecetarih());
            sqlcmd.Parameters.AddWithValue("@enddate", a );
            sqlcmd.Parameters.AddWithValue("@sinavID", ddlSinavlar.SelectedValue);
          

            SqlParameter parameter = new SqlParameter("@durum", SqlDbType.Int);                  
            parameter.Direction = ParameterDirection.Output;        
            sqlcmd.Parameters.Add(parameter);
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcon.Open();
            sqlcmd.ExecuteNonQuery();
            int durum = Convert.ToInt32(sqlcmd.Parameters["@durum"].Value);
            sqlcon.Close();

            if (durum==1)
            {
                mail m = new mail();
                m.mailadress = "sinav@usadanismanlikegitim.com";
                m.port = 587;
                m._mails = txtEmail.Text;
                m.subject = "Deneme Sınavı İçin Katılım Linkiniz";
                m.pass = "22Home";
                m.login = "sinav@usadanismanlikegitim.com";
                m.host = "mail.usadanismanlikegitim.com";
                m.icerik = "<p>Sayin " + txtAdsoyad.Text + ",  Deneme Sinavina Başlamak İçin Lütfen Aşşağıdaki Linke Tıkalyınız <br /> <a href='" + reallink + "' >" + reallink + "</a>  <br />Başarılar</p>";
                m.mail_gonder();
                if (m.durum == 1)
                {
                    Response.Write("basarili");
                }
                else
                {
                    Response.Write("basarisiz");
                }
            } 

        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            enddate = Calendar1.SelectedDate.ToString();
            Button1.Enabled = true;
        }
    }
}